/*
 *  ViewController.cpp
 *  PrototypeTemplate
 *
 *  Created by david on 5/03/09.
 *
 *	MIT License Here.
 *
 * Demo iPhone app showing ftgles in action.
 */

#include "ViewController.h"

static FTFont *fonts[3];

static FTSimpleLayout layouts[3];

static int renderMode = FTGL::RENDER_FRONT | FTGL::RENDER_BACK;

static char *strings[3];


ViewController::ViewController(const char* path)
{
	char fontname[256];
	
	glDepthFunc(GL_LEQUAL);
	glClearDepthf(1.0);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	
	snprintf(fontname, 256, "%s/Diavlo_BLACK_II_37.otf", path);
	fonts[0] = new FTTextureFont(fontname);
	if(fonts[0]->Error())
    {
        printf("Could not load font `%s'\n", fontname);
    }
	fonts[0]->FaceSize(40);
	fonts[0]->CharMap(ft_encoding_unicode);
	
	layouts[0].SetLineLength(320.0f);
	layouts[0].SetLineSpacing(0.75f);
    layouts[0].SetFont(fonts[0]);
	layouts[0].SetAlignment(FTGL::ALIGN_CENTER);
	
	snprintf(fontname, 256, "%s/Gartentika.ttf", path);
	fonts[1] = new FTTextureFont(fontname);
	if (fonts[1]->Error())
	{
        printf("Could not load font `%s'\n", fontname);	
	}
	fonts[1]->FaceSize(60);
	fonts[1]->CharMap(ft_encoding_unicode);
	
	layouts[1].SetLineLength(320.0f);
	layouts[1].SetLineSpacing(0.75f);
    layouts[1].SetFont(fonts[1]);
	layouts[1].SetAlignment(FTGL::ALIGN_CENTER);
	
	snprintf(fontname, 256, "%s/brown_bear_funk.ttf", path);
	fonts[2] = new FTTextureFont(fontname);
	if (fonts[2]->Error())
	{
        printf("Could not load font `%s'\n", fontname);	
	}
	fonts[2]->FaceSize(40);
	fonts[2]->CharMap(ft_encoding_unicode);
	
	layouts[2].SetLineLength(320.0f);
	layouts[2].SetLineSpacing(0.75f);
    layouts[2].SetFont(fonts[2]);
	layouts[2].SetAlignment(FTGL::ALIGN_CENTER);
	
	// just a demo.
	strings[0] = (char *)malloc(64);
	strings[1] = (char *)malloc(64);
	strings[2] = (char *)malloc(64);
	
	snprintf(strings[0], 64, "FTGL ES on an iPhone!");
	snprintf(strings[1], 64, "I am a truetype font.");
	snprintf(strings[2], 64, "TRUE TYPE");
}


ViewController::~ViewController() 
{
	free(strings[0]);
	free(strings[1]);
	free(strings[2]);
	
	delete fonts[0];
	delete fonts[1];
	delete fonts[2];
}

static float angle = 0.0f;
void ViewController::Draw()
{
	ftglInitImmediateModeGL();
	glClearColor(0.0f, 0.0f, 0.25f, 1.0f);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	
	glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
	
	glPushMatrix();
	glOrthof(0.0f, 320.0f, 
			 0.0f, 480.0f, 
			 1000.0f, -1000.0f);
	
	glDisable(GL_DEPTH_TEST);
	
	glEnable(GL_BLEND);
	
	glMatrixMode(GL_MODELVIEW);
	
	glPushMatrix();
	
	glTranslatef(160.0f, 240.0f, 0.0f);
	
	glPushMatrix();
	
	glRotatef(angle, 1.0f, 1.0f, 1.0f);
	angle += 0.5f;
	if (angle >= 360.0f) angle = 0.0f;
	
	glTranslatef(-160.0f, -240.0f, 0.0f);
	
	glPushMatrix();
	glTranslatef(0.0f, 420.0f, 0.0f);
	glColor4f(1.0f, 0.6f, 0.3f, 1.0f);
	layouts[0].Render(strings[0], -1, FTPoint(), renderMode);
	glPopMatrix();
	
	glPushMatrix();
	glTranslatef(10.0f, 280.0f, 0.0f);
	glColor4f(0.4f, 0.8f, 0.0f, 1.0f);
	layouts[1].Render(strings[1], -1, FTPoint(), renderMode);
	glPopMatrix();
	
	glPushMatrix();
	glTranslatef(10.0f, 140.0f, 0.0f);
	glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
	layouts[2].Render(strings[2], -1, FTPoint(), renderMode);
	glPopMatrix();
	
	glPopMatrix();
	
	glPopMatrix();
	
	glDisable(GL_BLEND);
	
	glEnable(GL_DEPTH_TEST);
	
	glMatrixMode(GL_PROJECTION);
	glPopMatrix();
}

